<?php

namespace app\model;

class ShoppingCart extends ShoppingCartBase
{
    public function scopeUser()
    {
        $this->where('user_id', request()->user->id);
    }

    public function clear()
    {
        $shopping_cart = ShoppingCart::where('user_id', request()->user->id)->find();
        $shopping_cart->totalItems = 0;
        $shopping_cart->totalAmount = 0;
        $shopping_cart->save();
        ShoppingCartProduct::destroy(function ($query) use ($shopping_cart) {
            $query->where('shopping_cart_id', $shopping_cart->id);
        });
    }
}
